package com.cn.controller;

import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.Execution;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 请假审批
 */
@RestController
@RequestMapping("leave")
public class LeaveController {

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    /**
     * 请假审批
     * @param student
     * @return
     */
    @PostMapping("/runtimeServiceTest")
    public String runtimeServiceTest(){
        HashMap<String, Object> map = new HashMap<>();
        String id = "approval";
        runtimeService.startProcessInstanceByKey(id,map);
        return "成功";
    }
    /**
     * 根据名称查询代表任务
     */
    @GetMapping("/selectUserTask")
    public String selectUserTask(String name){
        Task task = taskService.createTaskQuery()
                .taskAssignee(name)
                .singleResult();
        return task.toString();
    }

    /**
     * 查询代办任务
     * @return
     */
    @GetMapping("/selectTask")
    public String selectTask(){
        List<Task> list = taskService.createTaskQuery()
                .list();
        return list.toString();
    }

    /**
     * 查询变量
     * @return
     */
    @GetMapping("/select")
    public String select(){
        List<Execution> list = runtimeService.createExecutionQuery().list();
        Map<String, Object> map = null;
        for (Execution execution : list) {
           map = runtimeService.getVariables(execution.getId());
        }
        return map.toString();
    }


}
